package com.msmall.base.pager;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class WmlPaginationSupport extends PaginationSupport {

	private static final long serialVersionUID = -504539100094953674L;
	protected transient final Log log = LogFactory.getLog(getClass());
	/*
	public static final String FIRST = "<fmt:message key='plat.pagination.first' />";
	public static final String NEXT = "<fmt:message key='plat.pagination.next' />";
	public static final String PREVIOUS = "<fmt:message key='plat.pagination.previous' />";
	public static final String LAST = "<fmt:message key='plat.pagination.last' />";
	public static final String NOTE = "<fmt:message key='plat.pagination.note' />";
	*/
	public static final String FIRST = Messages.getMessage("plat.pagination.first");
	public static final String NEXT = Messages.getMessage("plat.pagination.next");
	public static final String PREVIOUS = Messages.getMessage("plat.pagination.previous");
	public static final String LAST = Messages.getMessage("plat.pagination.last");
	public static final String NOTE = Messages.getMessage("plat.pagination.note");
	
	public static final String HREF_BEGIN = "<a href=\"";
	public static final String HREF_MIDDLE = "\">";
	public static final String HREF_END = "</a>";
	public static final String WML_AND = "&#38;";
	public static final String TABLE_BEGIN = "<table>";
	public static final String TABLE_END = "</table>";
	public static final String TR_BEGIN = "<tr>";
	public static final String TR_END = "</tr>";
	public static final String TD_BEGIN = "<td>";
	public static final String TD_END = "</td>";
	public static final String PAGE_PAGEINDEX = "page.pageIndex=";
	public static final String PAGE_PAGESIZE = "page.pageSize=";
	public static final String JUMP_INPUT_NAME = "wml_page_pageIndex";
	public static final String JUMP_INPUT = "<input size=\"2\" name=\"" + JUMP_INPUT_NAME + "\" />";
	//public static final String JUMP_INPUT = "<input name=\"bbb_xxx\" /><setvar name=\"wml_page_pageIndex\" value=\"$(bbb_xxx - 1)\"/>";
	public static final String JUMP_ANCHOR_BEGIN = "<anchor>";
	public static final String JUMP_ANCHOR_END = "</anchor>";
	public static final String JUMP_GO_BEGIN = "<go method=\"get\" href=\"";
	public static final String JUMP_GO_MIDDLE = "\" >";
	public static final String JUMP_GO_END = "</go>";
	public static final String JUMP_SUBMIT = "GO";
	public static final String JUMP_POSTFIELD_BEGIAN = "<postfield name=\"";
	public static final String JUMP_POSTFIELD_MIDDLE = "\" value=\"";
	public static final String JUMP_POSTFIELD_END = "\" />";
	
	
	private String url;
	
	private String pageString;
	
	private int style = 1;
	//请求的参数
	private Map parameters;
	
	//如果有参数需要被替换
	private Map updateParams;
	
	private Locale locale = Locale.getDefault();
	
	public void initPageString(ServletRequest request) {
		initUrl(request);
		
		StringBuffer buffer = new StringBuffer();
		if(style == 1){
			buffer.append(this.TABLE_BEGIN)
			.append(this.TR_BEGIN)
			.append(this.createFirst(style))
			.append(this.createNext(style))
			.append(this.createPrev(style))
			.append(this.createLast(style))
			.append(this.createComment(style))
			.append(this.createJump(style))
			.append(this.TR_END)
			.append(this.TABLE_END);
		}
		
		this.setPageString(buffer.toString());
		log.info(buffer.toString());
	}
	protected String createFirst(int style){
		StringBuilder builder= new StringBuilder();
		builder.append(this.TD_BEGIN)
		.append(this.HREF_BEGIN)
		.append(this.getUrlWithParamter()).append(this.PAGE_PAGEINDEX).append(1)
		.append(this.HREF_MIDDLE)
		.append(FIRST)
		.append(this.HREF_END)
		.append(this.TD_END);
		return builder.toString();
	}
	protected String createNext(int style){
		StringBuilder builder= new StringBuilder();
	//	if(this.getTotalPage() - this.getPageIndex() > 0){
		if(this.getTotalPage() - this.getPageIndex() > 0){
			if(style == 1){
				builder.append(this.TD_BEGIN)
					.append(this.HREF_BEGIN)
					.append(this.getUrlWithParamter()).append(this.PAGE_PAGEINDEX).append(this.getPageIndex() + 1)
					.append(this.HREF_MIDDLE)
					.append(this.NEXT)
					.append(this.HREF_END)
					.append(this.TD_END);
			}
		}
		return builder.toString();
	}
	protected String createPrev(int style){
		StringBuilder builder= new StringBuilder();
	//	if(this.getPageIndex() - 1 >= 0){
		if(this.getPageIndex() - 1 > 0){
			if(style == 1){
				builder.append(this.TD_BEGIN)
				.append(this.HREF_BEGIN)
				.append(this.getUrlWithParamter()).append(this.PAGE_PAGEINDEX).append(this.getPageIndex() - 1)
				.append(this.HREF_MIDDLE)
				.append(this.PREVIOUS)
				.append(this.HREF_END)
				.append(this.TD_END);
			}
		}
		
		return builder.toString();
	}
	protected String createLast(int style){
		StringBuilder builder= new StringBuilder();
		if(style == 1){
			builder.append(this.TD_BEGIN)
			.append(this.HREF_BEGIN)
			.append(this.getUrlWithParamter()).append(this.PAGE_PAGEINDEX).append(this.getTotalPage())
			.append(this.HREF_MIDDLE)
			.append(this.LAST)
			.append(this.HREF_END)
			.append(this.TD_END);
		}
		return builder.toString();
	}
	protected String createComment(int style){
		StringBuilder builder= new StringBuilder();
		if(style == 1){
			builder.append(this.TD_BEGIN)
		//	.append(this.getPageIndex() + 1)
			.append(this.getPageIndex())
			.append("/")
			.append(this.getTotalPage())
			.append(this.TD_END);
		}
		return builder.toString();
	}
	protected String createJump(int style){
		StringBuilder builder= new StringBuilder();
		if(style == 1){
			builder.append(this.TD_BEGIN)
			.append(this.JUMP_INPUT)
			.append(this.JUMP_ANCHOR_BEGIN)
			.append(this.JUMP_GO_BEGIN)
			.append(this.getUrl())
			.append(this.JUMP_GO_MIDDLE);
			if(null != this.parameters && parameters.size() > 0){
				Iterator keys = parameters.keySet().iterator();
				while(keys.hasNext()){
					String key = (String) keys.next();
					builder.append(this.JUMP_POSTFIELD_BEGIAN)
					.append(key)
					.append(this.JUMP_POSTFIELD_MIDDLE);
					if("page.pageIndex".equals(key)){ // 修改当前页
						builder.append("$(" + JUMP_INPUT_NAME + ")");
					}else{
						builder.append(parameters.get(key));
					}
					builder.append(this.JUMP_POSTFIELD_END);
				}
			}else{
				builder.append(this.JUMP_POSTFIELD_BEGIAN)
				.append("page.pageIndex")
				.append(this.JUMP_POSTFIELD_MIDDLE)
				.append("$(" + JUMP_INPUT_NAME + ")")
				.append(this.JUMP_POSTFIELD_END);
			}
			builder.append(this.JUMP_GO_END)
			.append(this.JUMP_SUBMIT)
			.append(this.JUMP_ANCHOR_END)
			.append(this.TD_END);
		}
		return builder.toString();
	}
	public void initUrl(ServletRequest request){
		HttpServletRequest req = (HttpServletRequest)request;
		
		Enumeration<String> em = req.getParameterNames();
		if(null != em && em.hasMoreElements()){
			while(em.hasMoreElements()){ // 保留翻页的搜素条件
				String name = em.nextElement();
				if(null == parameters)
					parameters = new HashMap();
				parameters.put(name, req.getParameter(name));
				log.info("---->" + name + "     " + req.getParameter(name));
			}
		}
		String sessionId = ";jsessionid=" + req.getSession().getId();
		setUrl(req.getRequestURI() + sessionId);
	}
	
	// without pageIndex
	public String getUrlWithParamter(){
		StringBuilder builder = new StringBuilder();
		builder.append(this.url);
		if(null != this.parameters && parameters.size() > 0){
			builder.append("?");
			Iterator keys = parameters.keySet().iterator();
			Iterator updates = null;
			if(null != updateParams && !updateParams.isEmpty())
				updates = updateParams.keySet().iterator();
			while(keys.hasNext()){
				String key = (String) keys.next();
				if("page.pageIndex".equals(key)){ // 修改当前页
					continue;
				}
				if(updates != null){
					while(updates.hasNext()){ //如果有参数需要被替换
						String uKey = (String)updates.next();
						if(uKey.equals(key)){
							parameters.put(key, updateParams.get(uKey));
						}
					}
				}
				builder.append(key).append("=")
					.append(parameters.get(key))
					.append(this.WML_AND);
			}
		}else{
			builder.append("?").append(this.PAGE_PAGESIZE).append(this.getPageSize()).append(this.WML_AND);
		}
		return builder.toString();
	}

	public void updateParams(String name, Object value){
		if(null == updateParams)
			updateParams = new HashMap();
		updateParams.put(name, value);
	}
	public void refreshUpdateParams(){
		this.updateParams = null;
	}
	public String getPageString() {
		return pageString;
	}
	
	public void setPageString(String pageString) {
		this.pageString = pageString;
	}

	public int getStyle() {
		return style;
	}

	public void setStyle(int style) {
		this.style = style;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

}
